package bscorp.appbase.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import bscorp.appbase.AppManager;
import bscorp.appbase.R;
import bscorp.appbase.adapter.MainAdapter;
import bscorp.appbase.bmo.OrderBmo;
import bscorp.appbase.divider.EnvironmentMonitorItemDecoration;
import bscorp.appbase.model.Order;
import bscorp.appbase.model.User;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    final int WAIT_DATA = 1;
    final int DOING_DATA = 2;
    final int FINISH_DATA = 3;
    private User mUser = AppManager.LOCAL_LOGINED_USER;
    RecyclerView rv_list;
    List<Order> mOrders = new ArrayList<Order>();
    MainAdapter mAdapter;
    Toolbar toolbar;
    TextView tv_name;

    int showDateType = WAIT_DATA;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("待处理工单");
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        if (mUser.getType().equals(User.TYPE_USER)) {
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(MainActivity.this, AddOrderActivity.class));
                }
            });
        } else {
            fab.setVisibility(View.GONE);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        View headView = navigationView.getHeaderView(0);
        tv_name = (TextView) headView.findViewById(R.id.tv_name);
        tv_name.setText(mUser.getName());

        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        rv_list.addItemDecoration(new EnvironmentMonitorItemDecoration(MainActivity.this, 10));
        mAdapter = new MainAdapter(R.layout.activity_main_order_item, mOrders);
        mAdapter.openLoadAnimation();
        rv_list.setAdapter(mAdapter);
        rv_list.setItemAnimator(new DefaultItemAnimator());
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(MainActivity.this, OrderDetailActivity.class);
                intent.putExtra("order", mOrders.get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataByType(showDateType);
    }

    public void getDataByType(int type) {
        switch (type) {
            case WAIT_DATA: {
                mOrders.clear();
                mOrders.addAll(new OrderBmo().getOrderByType(Order.ORDER_NEW));
                break;
            }
            case DOING_DATA: {
                mOrders.clear();
                mOrders.addAll(new OrderBmo().getOrderByReportUserIdAndType(mUser.getId() + "", Order.ORDER_WAIT_REPAIRE));
                mOrders.addAll(new OrderBmo().getOrderByReportUserIdAndType(mUser.getId() + "", Order.ORDER_WAIT_CHECK));
                mOrders.addAll(new OrderBmo().getOrderByReportUserIdAndType(mUser.getId() + "", Order.ORDER_AGAIN_REPAIRE));
                mOrders.addAll(new OrderBmo().getOrderByRepaireUserIdAndType(mUser.getId() + "", Order.ORDER_WAIT_REPAIRE));
                mOrders.addAll(new OrderBmo().getOrderByRepaireUserIdAndType(mUser.getId() + "", Order.ORDER_WAIT_CHECK));
                mOrders.addAll(new OrderBmo().getOrderByRepaireUserIdAndType(mUser.getId() + "", Order.ORDER_AGAIN_REPAIRE));
                break;
            }
            case FINISH_DATA: {
                mOrders.clear();
                mOrders.addAll(new OrderBmo().getOrderByReportUserIdAndType(mUser.getId() + "", Order.ORDER_SUCCESS));
                mOrders.addAll(new OrderBmo().getOrderByRepaireUserIdAndType(mUser.getId() + "", Order.ORDER_SUCCESS));
                break;
            }
        }

        mAdapter.setNewData(mOrders);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            startActivity(new Intent(MainActivity.this, AddDeviceActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_order_wait) {
            if (showDateType != WAIT_DATA) {
                toolbar.setTitle("待处理工单");
                showDateType = WAIT_DATA;
                getDataByType(showDateType);
            }
        } else if (id == R.id.nav_order_going) {
            if (showDateType != DOING_DATA) {
                toolbar.setTitle("进行中工单");
                showDateType = DOING_DATA;
                getDataByType(showDateType);
            }
        } else if (id == R.id.nav_order_finish) {
            if (showDateType != FINISH_DATA) {
                toolbar.setTitle("已完成工单");
                showDateType = FINISH_DATA;
                getDataByType(showDateType);
            }
        } else if (id == R.id.nav_device_list) {
            startActivity(new Intent(MainActivity.this, DevicesListActivity.class));
        } else if (id == R.id.nav_login_out) {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
